<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Strict//EN">
<html>

<head>
<meta http-equiv="Content-Language" content="en-us">
<title>Vision callback functions</title>
<link rel="stylesheet" type="text/css" href="../style.css">
</head>

<body>

<div align="center">
<table class=allEncompassingTable >
 <tr>
  <td >
<p><a href="../index.html" TARGET="_top"><img src="images/homeImg.png"></a></p>



<h1>Vision callback functions</h1>

<p><a href="childScripts.htm">Child script</a>s, or <a href="customizationScripts.htm">customization scripts</a> can include a vision callback function (which is one of many <a href="callbackFunctions.htm#systemCallbacks">system callback functions</a>), when associated with a <a href="visionSensors.htm">vision sensor</a>. When present for a given vision sensor, then the system will call the callback function everytime a new image was acquired or applied, allowing the user to perform image processing. This is the case with following API functions: <a href="regularApi/simHandleVisionSensor.htm">sim.handleVisionSensor</a>, <a href="regularApi/simCheckVisionSensor.htm">sim.checkVisionSensor</a>, <a href="regularApi/simCheckVisionSensorEx.htm">sim.checkVisionSensorEx</a>, <a href="regularApi/simSetVisionSensorImage.htm">sim.setVisionSensorImage</a>, and <a href="regularApi/simSetVisionSensorCharImage.htm">sim.setVisionSensorCharImage</a>.</p>
<p>Some conditions apply as to the location of the vision callback function: if a vision callback function is present in a child script as well as in a customization script, both attached to the vision sensor, then the child script will be called first, and the customization script second.</p>

<p>Following represents an empty  vision callback function:<br>
</p>

<pre class=lightRedBox>
function <strong>sysCall_vision</strong>(inData)
    -- We have:
    -- inData.handle : the handle of the vision sensor.
    -- inData.resolution : the x/y resolution of the vision sensor
    -- inData.clippingPlanes : the near and far clipping planes of the vision sensor
    -- inData.viewAngle : the view angle of the vision sensor (if in persp. proj. mode)
    -- inData.orthoSize : the ortho size of the vision sensor (if in orth. proj. mode)
    -- inData.perspectiveOperation : true if the sensor is in persp. proj. mode

    local outData={}
    outData.trigger=false -- true if the sensor should trigger
    outData.packedPackets={} -- a table of packed packets. Can be accessed via e.g. sim.readVisionSensor
    return outData
end
</pre>

<br>
<p>Image processing can be performed by using various API functions. The <a href="visionPlugin.htm">vision plugin</a> exports a few very simple image processing functions. Many more image processing functions are supported via the <a href="simIM.htm?view=alphabetical">image plugin</a> (OpenCV wrapper).</p>
<p>Following represents a simple edge detection vision callback function, that triggers and returns a packet of data  (based on the vision plugin functions):<br>
</p>

<pre class=lightRedBox>
function <strong>sysCall_vision</strong>(inData)
    simVision.sensorImgToWorkImg(inData.handle)
    simVision.edgeDetectionOnWorkImg(inData.handle,0.1)
    simVision.workImgToSensorImg(inData.handle)

    local outData={}
    outData.trigger=true
    local packetData={1.0,42.123,129.3}
    outData.packedPackets={sim.packFloatTable(packetData)}
    return outData
end
</pre>


<br>
<p>Following represents a vision callback function, that draws a circle onto the acquired image (based on the image plugin functions):<br>
</p>

<pre class=lightRedBox>
function <strong>sysCall_vision</strong>(inData)
    local imgHandle=simIM.readFromVisionSensor(inData.handle)
    local center={inData.resolution[1]/2,inData.resolution[2]/2}
    local radius=(inData.resolution[1]+inData.resolution[2])/8
    simIM.circle(imgHandle,center,radius,{255,0,255},4)
    simIM.writeToVisionSensor(imgHandle,inData.handle)
    simIM.destroy(imgHandle)
end</pre>

<br>
<br>
 </tr>
 <tr>
   <td > 
 </tr>
</table> 
</div>  
  
  
</body>

</html>
